<script setup>
import { useRouter, useRoute } from 'vue-router'
import { ref, onMounted } from 'vue'
import { getPicCode, getMsgCode, CodeLogin } from '@/apis/login'
import { useUserStore } from '@/stores/user'
const userStore= useUserStore()
const router = useRouter()
const route = useRoute()
defineOptions({
  name: 'LoginIndex'
})
const picUrl = ref('') // 图形验证码图片
const picKey = ref('') // 图形验证码唯一标识
const picCode = ref('') // 输入的图形验证码  与库里面的唯一标示发给后端比较
const mobile = ref() // 手机号
const msgCode = ref() // 短信验证码
const timer = ref(null) //定时器
const totalSecond = ref(60) // 总秒数
const second = ref(60) // 当前秒数
// 获取图形验证码
const loadPicCode = async() => {
  const { data: { base64, key } } = await getPicCode()
  picUrl.value = base64
  picKey.value = key
}
// 获取短信验证码前需要校验手机号和图形验证码是否合法
const validFn = () => {
  if (!/^1[3-9]\d{9}$/.test(mobile.value)) {
    // eslint-disable-next-line no-undef
    showToast('请输入正确的手机号')
    return false
  }
  if (!/^\w{4}$/.test(picCode.value)) {
    // eslint-disable-next-line no-undef
    showToast('请输入正确的图形验证码')
    return false
  }
  return true
}
// 获取短信验证码
const loadMsgCode = async() => {
  if (!validFn()) {
    return
  }
  if (!timer.value && totalSecond.value === second.value) {
    const res = await getMsgCode(picCode.value, picKey.value, mobile.value)
    console.log(res)
    // eslint-disable-next-line no-undef
    showToast('短信发送成功，请注意查收！')
    timer.value = setInterval(() => {
      second.value--
      if (second.value <= 0) {
        clearInterval(timer) // 清除定时器
        timer.value = null // 重置定时器id
        second.value = totalSecond.value // 归位
      }
    }, 1000)
  }
}
// 登录验证
const login = async() => {
  if (!validFn()) {
    return
  }
  if (!/^\d{6}$/.test(msgCode.value)) {
    // eslint-disable-next-line no-undef
    showToast('请输入正确的验证码')
  }
  const res = await CodeLogin(mobile.value, msgCode.value)
  console.log(res)
  userStore.setUserInfo(res.data)
  // eslint-disable-next-line no-undef
  showToast('登录成功')

  const url = route.query.backUrl || '/'
  router.replace(url)
}
onMounted(() => {
  loadPicCode()
})

const onClickLeft = () => {
  router.go(-1)
}
</script>

<template>
  <van-nav-bar title="会员登录" left-arrow @click-left="onClickLeft" />
  <div class="container">
    <div class="title">
      <h3>手机号登录</h3>
      <p>未注册的手机号登录后将自动注册</p>
    </div>

    <div class="form">
      <div class="form-item">
        <input v-model="mobile" class="inp" maxlength="11" placeholder="请输入手机号"  type="text">
      </div>
      <div class="form-item">
        <input v-model="picCode" class="inp" maxlength="4" placeholder="请输入图形验证码"  type="text">
        <img v-if="picUrl" :src="picUrl" alt="" @click="loadPicCode">
      </div>
      <div class="form-item">
        <input v-model="msgCode" class="inp" maxlength="11" placeholder="请输入短信验证码"  type="text">
        <button @click="loadMsgCode">
          {{ second === totalSecond ? '获取验证码' : second + '秒后重新发送' }}
        </button>
      </div>
    </div>

    <div class="login-btn" @click="login">登录</div>
  </div>
</template>

<style lang="less" scoped>
:deep(.van-nav-bar__title) {
  font-weight: normal !important;
}
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>